<?php
include 'include/functions.php';
dbConnect();
//check to see if a Weeknum value was passed, if not, set WeekNum to CurrWeek.
if (($Weeknum == NULL) || ($Weeknum == ""))
{
	$Weeknum = getWeek();
}						
//check to make sure $Weeknum > 1 AND $Weeknum < 21
$Weeknum = checkWeekBounds($Weeknum);

//Write SQL Query to extract specified data
$SQL = "SELECT * FROM NHLSchedule Where (WEEK=".$Weeknum.") AND (Winner != ' ')";
//Open Record Set using SQL Query
$result = mysql_query($SQL) or die("Cannot query the database.<br/>");

echo "contents|<table bgcolor=\"#FFFFFF\" border=\"1\" callpadding=\"5\" cellspacing=\"0\" width=\"100%\" align=\"center\">";

$query="SELECT * FROM NHLSchedule Where (WEEK=".$Weeknum.") ORDER BY GameNum";
$doQuery=mysql_query($query);
$numrows=mysql_num_rows($doQuery);
$num = 1;
if($numrows > 0)
{
	while($results=mysql_fetch_array($doQuery))
	{
		//Fill Arrays With Data
		$homeTeams[$num]=$results['Home'];
		$awayTeams[$num]=$results['Away'];
		$winners[$num]=$results['Winner'];
		$num++;
	}
	
	//Print Away Line
	echo "<tr bgcolor=\"#FFFFFF\"><td><b>Away</b></td>";
	foreach($awayTeams as $team)
	{
	  echo "<td width=\"5\" align=\"center\"><img src=\"../images/small/".$team.".gif\"></td>";
	}
	echo "<td rowspan=\"3\" valign=\"center\" align=\"center\"><b>Week<br/>$Weeknum<br/>Score</b></td></tr>";
	
	//Print @ Line
	echo "<tr bgcolor=\"#FFFFFF\"><td>&nbsp;</td>";
	foreach($homeTeams as $team)
	{
	  echo "<td width=\"5\" align=\"center\">@</td>";
	}
	echo "</tr>";
	
	//Print Home Line
	echo "<tr bgcolor=\"#FFFFFF\" bordercolor=\"#172693\"><td><b>Home</b></td>";
	foreach($homeTeams as $team)
	{
	  echo "<td width=\"5\" align=\"center\"><img src=\"../images/small/".$team.".gif\"></td>";
	}
	echo "</tr>";
	
	$cols = $numrows + 2;
	echo "<tr height=\"1\" bgcolor=\"#FFFFFF\"><td colspan=\"$cols\" bgcolor=\"#FF0000\"><span style=\"font-size: 8px;\"&nbsp;</td></tr>";
	
	//Print Winner Line
	echo "<tr bgcolor=\"#FFFFFF\"><td><span style=\"color: rgb(255, 0, 0); font-weight: bold;\">Winner</span></td>";
	foreach($winners as $team)
	{
		if($team != "")
		{
			echo "<td width=\"5\" align=\"center\"><img src=\"../images/small/".$team.".gif\"></td>";
		}
		else
		{
			echo "<td width=\"5\" align=\"center\">&nbsp;</td>";
		}
	}
	echo "<td>&nbsp;</td></tr>";
	echo "<tr height=\"1\"><td colspan=\"$cols\" bgcolor=\"#FF0000\"><span style=\"font-size: 8px;\"&nbsp;</td></tr>";
	//Print Rows For User & Picks
	
	//Get Data From Users Who Picked
	$UserPickedSQL = "SELECT NHLPicks.*, NHLUsers.username, NHLUsers.ID FROM NHLPicks, NHLUsers WHERE ((NHLPicks.Week = ".$Weeknum.") AND (NHLPicks.UserID = NHLUsers.ID))  ORDER BY NHLPicks.UserID ASC";
	$UserPickedresult = mysql_query($UserPickedSQL) or die("Cannot query the database.<br/>");
	$row = 0;
	do{
	
		if ($myrow["UserID"] != NULL)
		{
			//increment row for the row color
			$row++;
			echo "<tr bgcolor=\"".getrowColor($row)."\">";
			echo "<td><a href=\"members.php?ID=".$myrow["UserID"]."\" class=\"permLink\">".$myrow["username"]."</a></td>\n";
			$weekScore = 0;
			for($j = 1; $j <= $numrows; $j++)
			{
				$pick = $myrow["Game".$j];

				if($winners[$j])
				{
					if($pick == $winners[$j])
					{
						echo "<td width=\"5\" align=\"center\"><img src=\"../images/small/".$pick.".gif\" /></td>\n";
						$weekScore++;
					}
					else
					{
						echo "<td width=\"5\" align=\"center\"><img src=\"../images/red-x.gif\" /></td>\n";
					}
				}
				else
				{
					echo "<td width=\"5\" align=\"center\">&nbsp;</td>\n";
				}
				
			}
			echo "<td align=\"center\"><span style=\"color: rgb(255, 0, 0); font-weight: bold; font-size: 16px;\">$weekScore</span></td></tr>\n";
		}
		
	}while($myrow = mysql_fetch_array($UserPickedresult));
	
}
else
{
	print '<tr bgcolor=\"#FFFFFF\"><td colspan=\"$cols\" align=\"center\">No Games Set for This Week Yet.</td></tr>';
}
//Close table
echo "</table>";
?>